package com.ccnu.demojava.mq.kafka;

import org.apache.kafka.clients.admin.*;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.internals.Topic;

import java.util.*;
import java.util.concurrent.ExecutionException;

public class RKafkaProducer {
    public static void main(String[] args) throws Exception {
        String topicName = "sim_req";
        // 设置kafka代理的属性
        Properties props = new Properties();
        //Assign localhost id
        props.put("bootstrap.servers", "172.20.99.26:9092");
        //Set acknowledgements for producer requests.
        props.put("acks", "all");
        //If the request fails, the producer can automatically retry,
        props.put("retries", 0);
        //Specify buffer size in config
        props.put("batch.size", 16384);
        //Reduce the no of requests less than 0
        props.put("linger.ms", 1);
        //The buffer.memory controls the total amount of memory available to the producer for buffering.
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        // 创建生产者
        Producer<String, String> producer = new KafkaProducer<String, String>(props);
//        createTopic(props, "kafka_topic", 80);
//        String stMsg = new String("ABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHAD");
//        String stMsg = new String("ABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADAGKDSBADAHBNMDAHAATATQWTQHQHBASBADAHAHAATATQWTQHQHBSDGAYQHQABC0fdafgQHBSDGAYQHQABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQ");
//        String stMsg = new String("ABCDEFEGRGDGAGASDGASGAHADASBADAHAHAATATQWTQHQHBSDGAYQHQABCDEFEGRGDGAGASddgbDGASGAHADASBADAHAHAATATQWTQHQHKGHDBSDGAYQHQABCDEFEGRG");
        String stMsg = new String("{ordernew}");
        Date startDT = new Date();
        System.out.println("timer:" + String.valueOf(startDT.getTime()));
        // 定义生产者
        for (int i = 0; i < 1000000; i++) {
            producer.send(new ProducerRecord<String, String>(topicName,
                    Integer.toString(i), stMsg));
        }
        Date endDT = new Date();
        System.out.println("cost time:" + String.valueOf(endDT.getTime() - startDT.getTime()) + "ms");
        System.out.println("Message sent successfully");
        Thread.sleep(30*1000);
        producer.close();
    }

    public static void createTopic(Properties props, String topicName){
        createTopic(props, topicName, 1);
    }

    public static void createTopic(Properties props, String topicName, int partitionNumber){
        Short replicationFactor = 1;
        // 创建一个管理客户端
        AdminClient ac = AdminClient.create(props);
        NewTopic ntc = new NewTopic(topicName, partitionNumber, replicationFactor);

        ListTopicsResult topicList = ac.listTopics();
        KafkaFuture<Set<String>> topicNames = topicList.names();
        while(true) {
            if (topicNames.isDone()) {
                Set<String> topicNameSet = null;
                try {
                    topicNameSet = topicNames.get();
                    if (topicNameSet.contains(topicName)) {
                        Map<String,NewPartitions> newPartitions =new HashMap<>();
                        newPartitions.put(topicName,NewPartitions.increaseTo(80));
                        ac.createPartitions(newPartitions);
                    } else {
                        CreateTopicsResult topicsResult = ac.createTopics(Arrays.asList(ntc));
                        topicsResult.all().get();
                        System.out.println("CreateTopicsResult:" + topicsResult.values());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                break;
            }
            // 失败也退出
            if (topicNames.isCancelled() || topicNames.isCompletedExceptionally()) {
                break;
            }
        }

        // 休眠一会
        try {
            Thread.sleep(10*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
